/**
 * 
 */
package com.bestbuy.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bestbuy.pojo.Catalog;
import com.bestbuy.pojo.Product;
import com.bestbuy.service.CatalogService;
import com.bestbuy.service.ProductService;

/**
 * @author Tuan
 *
 */
@Controller
public class SearchController {
	@Autowired
	private CatalogService catalogService;
	@Autowired
	private ProductService productService;
	
	@RequestMapping(value = { "/search.do" },method = RequestMethod.GET)
	public String SearchMenu(@RequestParam(value="tbKeyWord") String keyword,
			@RequestParam(value="idcatogory") String catalogid,
			@RequestParam(value="page") String pagecurrent,Model model) 
	{
		
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		//int page = productService.TotalPage(keyword, catalogid);
		int page = productService.countProduct(keyword, catalogid, "0");
		model.addAttribute("totalPage",page);
		//List<Product> listProduct = productService.Search(keyword, catalogid,pagecurrent);
		List<Product> listProduct = productService.Search(keyword, catalogid, pagecurrent, "0");
		model.addAttribute("products",listProduct);
		model.addAttribute("tbKeyWord", keyword);
		model.addAttribute("idcatogory", catalogid);
		model.addAttribute("pagecurrent",pagecurrent);
		model.addAttribute("Page", "Search");
		return "Main";
	}
	
//	@RequestMapping(value = { "/search.do" },method = RequestMethod.GET)
//	public String SearchText(
//			@RequestParam(value = "keysearch") String keySearch,
//			@RequestParam(value = "idcatogory") String idCatagory,
//			HttpSession session,Model model) 
//	{
//		
//		List<Catalog> listCatalog = catalogService.getListCatalog();
//		model.addAttribute("catalogs", listCatalog);
//		List<Product> listProduct = productService.Search(keySearch, idCatagory);
//		model.addAttribute("products",listProduct);
//		
//		model.addAttribute("Page", "Search");
//		return "Main";
//	}
	@RequestMapping(value = { "/searchprice.do" },method = RequestMethod.GET)
	public String SearchText(@RequestParam(value="startprice") String startPrice,
			@RequestParam(value="endprice") String endPrice,Model model) 
	{
		
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		//List<Product> listProduct = productService.Search(keySearch, idCatagory);
		//model.addAttribute("products",listProduct);
		
		model.addAttribute("Page", "Search");
		return "Main";
	}
}
